﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net.Mail;
using System.Web;
using System.Web.Mvc;
using FreightSoft.Models;

namespace FreightSoft.Controllers
{
    public class OrderLineController : Controller
    {
        private Entities db = new Entities();
        Classes.OzekiSMS sms = new Classes.OzekiSMS();

        //
        // GET: /OrderLine/

        public ActionResult Index(decimal id = 0)
        {
            var order_line = db.ORDER_LINE.Where(x => x.ORDER_ID == id).Include(o => o.CUSTOMER_ORDER).Include(o => o.PART).OrderBy(x => x.ORDER_ID).ThenBy(x => x.LINE_ID);
            ViewBag.ORDER_ID = id;
            return View(order_line.ToList());
        }

        //
        // GET: /OrderLine/Details/5

        public ActionResult Details(decimal id = 0)
        {
            ORDER_LINE order_line = db.ORDER_LINE.Find(id);
            if (order_line == null)
            {
                return HttpNotFound();
            }
            return View(order_line);
        }

        //
        // GET: /OrderLine/Create

        public ActionResult Create()
        {
            ViewBag.ORDER_ID = new SelectList(db.CUSTOMER_ORDER, "ORDER_ID", "ORDER_ID");
            ViewBag.PART_ID = new SelectList(db.PARTs, "PART_ID", "PART_DESC");
            return View();
        }

        //
        // POST: /OrderLine/Create

        [HttpPost]
        public ActionResult Create(ORDER_LINE order_line)
        {
            if (ModelState.IsValid)
            {
                PART part = db.PARTs.Find(order_line.PART_ID);
                order_line.LINE_ID = db.ORDER_LINE.Max(p => p.LINE_ID) + 1;
                order_line.NET_AMOUNT = part.UNIT_COST * order_line.QUANTITY;
                order_line.TAX_AMOUNT = order_line.NET_AMOUNT * 12.5m / 100;
                order_line.GROSS_AMOUNT = order_line.NET_AMOUNT + order_line.TAX_AMOUNT;


                db.ORDER_LINE.Add(order_line);
                db.SaveChanges();
                return RedirectToAction("Index", new { id = order_line.ORDER_ID });
            }

            ViewBag.ORDER_ID = new SelectList(db.CUSTOMER_ORDER, "ORDER_ID", "ORDER_ID", order_line.ORDER_ID);
            ViewBag.PART_ID = new SelectList(db.PARTs, "PART_ID", "PART_DESC", order_line.PART_ID);
            return View(order_line);
        }

        //
        // GET: /OrderLine/Edit/5

        public ActionResult Edit(decimal id = 0, decimal id2 = 0)
        {
            ORDER_LINE order_line = db.ORDER_LINE.Find(id, id2);
            if (order_line == null)
            {
                return HttpNotFound();
            }
            ViewBag.ORDER_ID = new SelectList(db.CUSTOMER_ORDER, "ORDER_ID", "ORDER_ID", order_line.ORDER_ID);
            ViewBag.PART_ID = new SelectList(db.PARTs, "PART_ID", "PART_DESC", order_line.PART_ID);
            return View(order_line);
        }

        //
        // POST: /OrderLine/Edit/5

        [HttpPost]
        public ActionResult Edit(ORDER_LINE order_line)
        {
            if (ModelState.IsValid)
            {
                db.Entry(order_line).State = EntityState.Modified;
                PART part = db.PARTs.Find(order_line.PART_ID);
                order_line.NET_AMOUNT = part.UNIT_COST * order_line.QUANTITY;
                order_line.TAX_AMOUNT = order_line.NET_AMOUNT * 12.5m / 100;
                order_line.GROSS_AMOUNT = order_line.NET_AMOUNT + order_line.TAX_AMOUNT;
                db.SaveChanges();
                return RedirectToAction("Index", new { id = order_line.ORDER_ID });
            }
            ViewBag.ORDER_ID = new SelectList(db.CUSTOMER_ORDER, "ORDER_ID", "ORDER_ID", order_line.ORDER_ID);
            ViewBag.PART_ID = new SelectList(db.PARTs, "PART_ID", "PART_DESC", order_line.PART_ID);
            return View(order_line);
        }

        //
        // GET: /OrderLine/Delete/5

        public ActionResult Delete(decimal id = 0, decimal id2 = 0)
        {
            ORDER_LINE order_line = db.ORDER_LINE.Find(id, id2);
            if (order_line == null)
            {
                return HttpNotFound();
            }
            return View(order_line);
        }

        //
        // POST: /OrderLine/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(decimal id = 0, decimal id2 = 0)
        {
            ORDER_LINE order_line = db.ORDER_LINE.Find(id, id2);
            db.ORDER_LINE.Remove(order_line);
            db.SaveChanges();
            return RedirectToAction("Index", new { id = order_line.ORDER_ID });
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }

        public ActionResult Plan(int id)
        {
            CUSTOMER_ORDER order = db.CUSTOMER_ORDER.Find(id);
            order.STATUS = "Planned";
            db.SaveChanges();
            return RedirectToAction("Index", new { id = id });
        }


        public ActionResult Release(int id)
        {
            CUSTOMER_ORDER order = db.CUSTOMER_ORDER.Find(id);
            TimeSpan ts = order.WANTED_DELIVERY_DATE - DateTime.Now;

            if (ts.Days > 5)
            {
                order.STATUS = "Released";
                order.STATUS = "Authorized";
                db.SaveChanges();
                string msg = "Your order " + order.ORDER_ID + " Has been authorized. Please proceed with the payments";
                sms.SendSMS(order.CUSTOMER.PHONE.ToString(), msg );
            }
            else
            {
                TempData["error"] = "Wanted delivery date is too early, can't find scheduled shipments.";
            }
            return RedirectToAction("Index", new { id = id });
        }


        public ActionResult Cancel(int id)
        {
            CUSTOMER_ORDER order = db.CUSTOMER_ORDER.Find(id);
            order.STATUS = "Cancelled";
            db.SaveChanges();
            return RedirectToAction("Index", new { id = id });
        }


        public ActionResult Authorize(int id)
        {
            CUSTOMER_ORDER order = db.CUSTOMER_ORDER.Find(id);
            order.STATUS = "Authorized";
            //db.SaveChanges();
            //db.OZEKIMESSAGEOUTs.Add(new OZEKIMESSAGEOUT { MSG = "Your order Has been authorized. Please proceed with the payments", SENDER = "0094727205458", RECEIVER = "0094715205458", MSGTYPE = "SMS:TEXT"});
            db.SaveChanges();
            sms.SendSMS(order.CUSTOMER.PHONE.ToString(), "Your order Has been authorized. Please proceed with the payments");
            return RedirectToAction("Index", new { id = id });
        }


        public ActionResult Invoice(int id)
        {
            CUSTOMER_ORDER order = db.CUSTOMER_ORDER.Find(id);
            order.STATUS = "Invoiced";
            db.SaveChanges();
            SendInvoiceToCustomer(order);
            //chrnlk
            
            string s = "Track your order with tracking no , "+ generateRefNo(order.ORDER_ID).ToString();
            sms.SendSMS(order.CUSTOMER.PHONE.ToString(), s);

            return RedirectToAction("Index", new { id = id });
        }


        private void SendInvoiceToCustomer(CUSTOMER_ORDER order)
        {
            MailMessage mail = new MailMessage();
            mail.To.Add(order.CUSTOMER.EMAIL);
            mail.Subject = String.Format("Payment Recieved, Order No: {0}", order.ORDER_ID);
            mail.Body = String.Format(@"Hi,

Your payment to Order No: {0} has been successfully received. Please use below link to see the invoice.
http://localhost:55882/CashPayment/Details/{1}
Order is being further processed for shipment.

Thank you,
MSC.", order.ORDER_ID, order.ORDER_ID);
            BusinessLogicLayer.ROUTE.Mail custMail = new BusinessLogicLayer.ROUTE.Mail();
            custMail.Send(mail);
        }

        private string generateRefNo(decimal orderNo)
        {
            string ord = "ms06z" + orderNo + "02";
            return ord;
        }
    }
}